package com.definesys.smecrailtransit.module.SiteDetails;

import android.content.Context;
import android.util.Log;

import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.ELeStatusBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleAttentionBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleErrorContentBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleFaultStationsBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleStationRunTimeBean;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;

import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleFaultStatisticsFragment.GET_SITE_ELE_FAULT_Fail;
import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleFaultStatisticsFragment.GET_SITE_ELE_FAULT_SUCCESS;
import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleRealTimeFragment.GET_ELE_STATUS_SUCCESS;
import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleRealTimeFragment.GET_ERROR_CONTENT_SUCCESS;
import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleRuntimelengthFragment.GET_SITE_RUN_TIME_FAIL;
import static com.definesys.smecrailtransit.module.SiteDetails.Fragment.EleRuntimelengthFragment.GET_SITE_RUN_TIME_SUCCESS;

/**
 * Created by xupeizuo on 2017/8/21.
 */

public class SiteDetailsPresenter extends SmecBasePresenter implements SiteDetailsContract{


    public SiteDetailsPresenter(Context mContext) {
        super(mContext);
    }

    @Override
    public void getRealTimeStatus(HashMap<String, Integer> map) {
        Observable<HttpResult<ArrayList<ELeStatusBean>>> observable= smecRailService.getRealTimeEleStatus(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<ELeStatusBean>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<ELeStatusBean>> arrayListHttpResult) {
                mRxbus.post(GET_ELE_STATUS_SUCCESS,arrayListHttpResult.getData());
            }
        });
    }

    @Override
    public void getErrorContent(HashMap<String, Object> map) {
        Observable<HttpResult<EleErrorContentBean>> observable=smecRailService.getEleErrorContent(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<EleErrorContentBean>>() {
            @Override
            public void onNext(HttpResult<EleErrorContentBean> eleErrorContentBeanHttpResult) {
                mRxbus.post(GET_ERROR_CONTENT_SUCCESS,eleErrorContentBeanHttpResult.getData());
            }
        });
    }

    @Override
    public void getSiteRunTime(HashMap<String, Object> map) {
        Observable<HttpResult<ArrayList<EleStationRunTimeBean>>> observable=smecRailService.getSiteRunTime(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<EleStationRunTimeBean>>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(GET_SITE_RUN_TIME_FAIL,new Object());
            }

            @Override
            public void onNext(HttpResult<ArrayList<EleStationRunTimeBean>> arrayListHttpResult) {
                Log.e("response",System.currentTimeMillis()+"");
                mRxbus.post(GET_SITE_RUN_TIME_SUCCESS,arrayListHttpResult.getData());
            }
        });
    }

    @Override
    public void getSiteEleFault(HashMap<String, Object> map) {
        Observable<HttpResult<EleFaultStationsBean>> observable=smecRailService.getSiteEleFault(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<EleFaultStationsBean>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(GET_SITE_ELE_FAULT_Fail,new Object());
            }

            @Override
            public void onNext(HttpResult<EleFaultStationsBean> eleFaultStationsBeanHttpResult) {
                mRxbus.post(GET_SITE_ELE_FAULT_SUCCESS,eleFaultStationsBeanHttpResult.getData());
            }
        });
    }

    /**
     * 获取关注数据
     * @return
     */
    public EleAttentionBean getAttention() {
        return (EleAttentionBean)realmManager.findFirst(EleAttentionBean.class);

    }

    /**
     * 改变关注站点
     * @param map
     */
    @Override
    public void updateAttention(HashMap<String, Object> map) {
        Observable<HttpResult> observable=smecRailService.updateAttention(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult>() {
            @Override
            public void onNext(HttpResult httpResult) {
                realmManager.deleteAll(EleAttentionBean.class);
                EleAttentionBean eleAttentionBean=new EleAttentionBean();
                eleAttentionBean.setStationId((Integer) map.get("stationId"));
                eleAttentionBean.setStationName((String) map.get("stationName"));
                realmManager.insertOrUpdate(eleAttentionBean);
            }
        });
    }
}
